namespace :svn do
  task :up do
    sh %(svn up)
  end

  task :add do
    sh %(svn st | grep "^?" | awk -F "      " '{printf "\\"%s\\"\\n", $2}' | xargs --no-run-if-empty svn add)
  end

  task :commit do
    sh %(svn stat --ignore-externals)
    require 'readline'
    pair = prompt_cached 'Pair'
    story = prompt_cached 'Story / Bug Number'
    comment = prompt_cached 'Comment'
    sh %(svn ci -m "#{pair} - ##{story} - #{comment}")
  end

  def prompt_cached(label)
    file = ".#{label.gsub(/\W/, '')}"
    value = File.read(file) if File.exist?(file)
    new_value = Readline.readline("#{label}#{value ? " [#{value}]" : ''}: ")
    if new_value.any?
      value = new_value
      File.open(file, 'w') {|f| f.write value }
    end
    value
  end
end